<?php
require(dirname(__FILE__) . "/../config/db_conf.inc");

// 生成标准的GUID
function guid(){
    if (function_exists('com_create_guid')){
        return com_create_guid();
    }else{
        mt_srand((double)microtime()*10000);//optional for php 4.2.0 and up.
        $charid = strtoupper(md5(uniqid(rand(), true)));
        $hyphen = chr(45);// "-"
        $uuid =
		substr($charid, 0, 8).$hyphen
            .substr($charid, 8, 4).$hyphen
            .substr($charid,12, 4).$hyphen
            .substr($charid,16, 4).$hyphen
            .substr($charid,20,12);
        return $uuid;
    }
}

// 获取客户端IP地址
function get_client_ip(){
	$ip = '';
	if (getenv("HTTP_CLIENT_IP"))
		$ip = getenv("HTTP_CLIENT_IP");
	else if(getenv("HTTP_X_FORWARDED_FOR"))
		$ip = getenv("HTTP_X_FORWARDED_FOR");
	else if(getenv("REMOTE_ADDR"))
		$ip = getenv("REMOTE_ADDR");
	else
		$ip = "获取IP地址失败";
	return $ip;
}

// 获取当前主题名称
function get_cur_theme()
{
	// 连接数据库
	global $g_dbhost;
	global $g_dbname;
	global $g_dba_username;
	global $g_dba_password;

	$dbconn = new mysqli($g_dbhost,$g_dba_username,$g_dba_password);
	if($dbconn->errno){
		exit($dbconn->_errno);
	}

	$dbconn->query("SET NAMES 'UTF8'");
	$dbconn->query("SET CHARACTER SET UTF8");
	$dbconn->query("SET CHARACTER_SET_RESULTS=UTF8'");

	//选择数据库
	$dbconn->select_db($g_dbname);

	//执行MySQL语句
	$s_sql = "select value from tb_sys_info where name ='cur_theme' limit 0,1 ;";
	$result=$dbconn->query($s_sql);
	if(!$result) {
		print(mysql_error());
		return "default";
	}

	$ret = "default";
	//提取数据
	if ($result) {
		while ( $row = $result->fetch_array()){
			$ret = $row['value'];
			break;
		}
		// 释放资源 
		$result->free();
	}

	// 关闭连接 
	$dbconn->close();

	return $ret;
}

// 获取用户已有权限列表
function get_auth_user($username)
{

	$auth_list = array();
	
	// 连接数据库
	global $g_dbhost;
	global $g_dbname;
	global $g_dba_username;
	global $g_dba_password;

	$dbconn = new mysqli($g_dbhost,$g_dba_username,$g_dba_password);
	if($dbconn->errno){
		exit($dbconn->_errno);
	}

	$dbconn->query("SET NAMES 'UTF8'");
	$dbconn->query("SET CHARACTER SET UTF8");
	$dbconn->query("SET CHARACTER_SET_RESULTS=UTF8'");

	//选择数据库
	$dbconn->select_db($g_dbname);

	//执行MySQL语句
	$s_sql ="call get_auth_user('{$username}');";
	$result=$dbconn->query($s_sql);
	if(!$result) {
		print(mysql_error());
		return $auth_list;
	}
	
	$s_sql ="select * from tb_tmp_ret;";
	$result=$dbconn->query($s_sql);
	if(!$result) {
		print(mysql_error());
		return $auth_list;
	}

	//提取数据
	if ($result) {
		while ( $row = $result->fetch_array()){
			$auth_list[] = $row['id_authority'];
		}
		// 释放资源 
		$result->free();
	}

	// 关闭连接 
	$dbconn->close();

	return $auth_list;
}

// 获取用户头像地址
function get_avatar_user($username)
{
	// 连接数据库
	global $g_dbhost;
	global $g_dbname;
	global $g_dba_username;
	global $g_dba_password;

	$dbconn = new mysqli($g_dbhost,$g_dba_username,$g_dba_password);
	if($dbconn->errno){
		exit($dbconn->_errno);
	}

	$dbconn->query("SET NAMES 'UTF8'");
	$dbconn->query("SET CHARACTER SET UTF8");
	$dbconn->query("SET CHARACTER_SET_RESULTS=UTF8'");

	//选择数据库
	$dbconn->select_db($g_dbname);

	$avtar = "";
	
	//执行MySQL语句
	$s_sql =" select ifnull(avatar,'') avatar from tb_user where name = '{$username}'; ";
	$result=$dbconn->query($s_sql);
	if(!$result) {
		print(mysql_error());
		return $avatar;
	}
	
	//提取数据
	if ($result) {
		while ( $row = $result->fetch_array()){
			$avatar = $row['avatar'];break;
		}
		// 释放资源 
		$result->free();
	}

	// 关闭连接 
	$dbconn->close();

	return $avatar;
}

// 获取用户信息
function get_user_info($username)
{
	// 连接数据库
	global $g_dbhost;
	global $g_dbname;
	global $g_dba_username;
	global $g_dba_password;

	$dbconn = new mysqli($g_dbhost,$g_dba_username,$g_dba_password);
	if($dbconn->errno){
		exit($dbconn->_errno);
	}

	$dbconn->query("SET NAMES 'UTF8'");
	$dbconn->query("SET CHARACTER SET UTF8");
	$dbconn->query("SET CHARACTER_SET_RESULTS=UTF8'");

	//选择数据库
	$dbconn->select_db($g_dbname);
	
	//执行MySQL语句
	$s_sql =" select * from tb_user where name = '{$username}'; ";
	$result=$dbconn->query($s_sql);
	if(!$result) {
		print(mysql_error());
		return false;
	}

	$user = array();
	
	//提取数据
	if ($result) {
		while ( $row = $result->fetch_array()){
			$user = $row;break;
		}
		// 释放资源 
		$result->free();
	}

	// 关闭连接 
	$dbconn->close();

	return $user;
}

// 添加新的文章
function add_new_art($username, $category, $title, $intro, $thumb, $slide, $labels, $content)
{
	// 连接数据库
	global $g_dbhost;
	global $g_dbname;
	global $g_dba_username;
	global $g_dba_password;

	$dbconn = new mysqli($g_dbhost,$g_dba_username,$g_dba_password);
	if($dbconn->errno){
		exit($dbconn->_errno);
	}

	$dbconn->query("SET NAMES 'UTF8'");
	$dbconn->query("SET CHARACTER SET UTF8");
	$dbconn->query("SET CHARACTER_SET_RESULTS=UTF8'");

	//选择数据库
	$dbconn->select_db($g_dbname);

	//执行SQL语句
	$s_guid = guid();
	
	$s_sql_labels_h = '';
	$s_sql_labels_b = '';
	$label_list = explode(' ',$labels);
	$s_sql_labels_h .= ' insert into tb_label_article(guid_art,label) ';
	$index = 0;
	foreach ($label_list as $label) {
		if (trim($label) != "") {
			if ($index > 0)	$s_sql_labels_b .= ' union all ';
			$s_sql_labels_b .= ' select ' . '\'' . $s_guid . '\' guid_art, ' . '\'' . $label . '\' label' ;
			$index += 1;
		}
	}
	$s_sql_labels = '';
	if ($s_sql_labels_b != '') {
		$s_sql_labels = $s_sql_labels_h . $s_sql_labels_b . ';';
	}
	
	$s_sql = ' insert into tb_article(username,id_category,guid,title,intro,thumb, slide, content, maketime) values(';
	$s_sql .= "'{$username}'," . "{$category}," . "'{$s_guid}'," . "'{$title}'," .
		"'{$intro}'," ."'{$thumb}'," . "'{$slide}'," . "'{$content}',"  . "now()" . '); ';
	$dbconn->query("SET NAMES 'UTF8'");
	$result=$dbconn->query($s_sql);
	if(!$result) {
		print($dbconn->errno);
		return "";
	}

	$dbconn->query("SET NAMES 'UTF8'");
	$result=$dbconn->query($s_sql_labels);
	if(!$result) {
		print('>>> 添加文章标签失败，错误号：' . $dbconn->errno . ' <<<');
	}
	
	// 关闭连接 
	$dbconn->close();

	return $s_guid;
}

// 获取文章分类列表
function get_category()
{
	// 连接数据库
	global $g_dbhost;
	global $g_dbname;
	global $g_dba_username;
	global $g_dba_password;

	$dbconn = new mysqli($g_dbhost,$g_dba_username,$g_dba_password);
	if($dbconn->errno){
		exit($dbconn->_errno);
	}

	$dbconn->query("SET NAMES 'UTF8'");
	$dbconn->query("SET CHARACTER SET UTF8");
	$dbconn->query("SET CHARACTER_SET_RESULTS=UTF8'");

	//选择数据库
	$dbconn->select_db($g_dbname);

	//执行MySQL语句
	$s_sql = " select * from tb_category order by dn; ";
	$result=$dbconn->query($s_sql);
	if(!$result) {
		print(mysql_error());
		return false;
	}

	$cates = array();
	//提取数据
	if ($result) {
		while ( $row = $result->fetch_array()){
			array_push($cates, $row);
		}
		// 释放资源 
		$result->free();
	}

	// 关闭连接 
	$dbconn->close();

	return $cates;
}

// 获取系统配置项值
function get_sys_info($itemname)
{
	// 连接数据库
	global $g_dbhost;
	global $g_dbname;
	global $g_dba_username;
	global $g_dba_password;

	$dbconn = new mysqli($g_dbhost,$g_dba_username,$g_dba_password);
	if($dbconn->errno){
		exit($dbconn->_errno);
	}

	$dbconn->query("SET NAMES 'UTF8'");
	$dbconn->query("SET CHARACTER SET UTF8");
	$dbconn->query("SET CHARACTER_SET_RESULTS=UTF8'");

	//选择数据库
	$dbconn->select_db($g_dbname);

	//执行MySQL语句
	$s_sql = "select value from tb_sys_info where name ='{$itemname}' limit 0,1 ;";
	$result=$dbconn->query($s_sql);
	if(!$result) {
		print($dbconn->errno);
		return "";
	}

	$ret = "";
	//提取数据
	if ($result) {
		while ( $row = $result->fetch_array()){
			$ret = $row['value'];
			break;
		}
		// 释放资源 
		$result->free();
	}

	// 关闭连接 
	$dbconn->close();

	return $ret;
}


// 获取幻灯文章($top条)列表
function get_slide_arts($top)
{
	// 连接数据库
	global $g_dbhost;
	global $g_dbname;
	global $g_dba_username;
	global $g_dba_password;

	if ($top < 1) return false;
	$s_top = $top;

	$dbconn = new mysqli($g_dbhost,$g_dba_username,$g_dba_password);
	if($dbconn->errno){
		exit($dbconn->_errno);
	}

	$dbconn->query("SET NAMES 'UTF8'");
	$dbconn->query("SET CHARACTER SET UTF8");
	$dbconn->query("SET CHARACTER_SET_RESULTS=UTF8'");

	//选择数据库
	$dbconn->select_db($g_dbname);

	//执行MySQL语句
	
	$s_sql = " select * from tb_article where trim(ifnull(slide, '')) <> '' order by maketime desc limit 0, {$s_top}; ";
	$result=$dbconn->query($s_sql);
	if(!$result) {
		print($dbconn->errno);
		return false;
	}

	$arts = array();
	//提取数据
	if ($result) {
		while ( $row = $result->fetch_array()){
			$arts[] = $row;
		}
		// 释放资源 
		$result->free();
	}

	// 关闭连接 
	$dbconn->close();

	if (count($arts) < 1) return false;
	return $arts;
}

// 获取编辑推荐文章($top条)列表
function get_chosen_arts($top)
{
	// 连接数据库
	global $g_dbhost;
	global $g_dbname;
	global $g_dba_username;
	global $g_dba_password;

	if ($top < 1) return false;
	$s_top = $top;

	$dbconn = new mysqli($g_dbhost,$g_dba_username,$g_dba_password);
	if($dbconn->errno){
		exit($dbconn->_errno);
	}

	$dbconn->query("SET NAMES 'UTF8'");
	$dbconn->query("SET CHARACTER SET UTF8");
	$dbconn->query("SET CHARACTER_SET_RESULTS=UTF8'");

	//选择数据库
	$dbconn->select_db($g_dbname);

	//执行MySQL语句
	
	$s_sql = "";
	$s_sql = <<<EOT
	select * from tb_article where guid in (
		select guid_art from (
			select distinct guid_art from tb_editor_choice order by choice_time desc limit 0, {$top}
		)as t
	)
EOT;
	$result=$dbconn->query($s_sql);
	if(!$result) {
		print($dbconn->errno);
		return false;
	}

	$arts = array();
	//提取数据
	if ($result) {
		while ( $row = $result->fetch_array()){
			$arts[] = $row;
		}
		// 释放资源 
		$result->free();
	}

	// 关闭连接 
	$dbconn->close();

	if (count($arts) < 1) return false;
	return $arts;
}

// 获取全部文章列表
function get_all_arts($s_cateid)
{
	// 连接数据库
	global $g_dbhost;
	global $g_dbname;
	global $g_dba_username;
	global $g_dba_password;

	$dbconn = new mysqli($g_dbhost,$g_dba_username,$g_dba_password);
	if($dbconn->errno){
		exit($dbconn->_errno);
	}

	$dbconn->query("SET NAMES 'UTF8'");
	$dbconn->query("SET CHARACTER SET UTF8");
	$dbconn->query("SET CHARACTER_SET_RESULTS=UTF8'");

	//选择数据库
	$dbconn->select_db($g_dbname);

	//执行MySQL语句
	$s_sql = "";
	$s_sql .= " select ";
	$s_sql .= " guid,username,title,intro,content,ifnull(thumb, '') thumb,slide,id_category,maketime ";
	$s_sql .= " from tb_article where id_category like \"{$s_cateid}\" order by maketime desc; ";
	$result=$dbconn->query($s_sql);
	if(!$result) {
		print($dbconn->errno);
		return false;
	}

	$arts = array();
	//提取数据
	if ($result) {
		while ( $row = $result->fetch_array()){
			$arts[] = $row;
		}
		// 释放资源 
		$result->free();
	}

	// 关闭连接 
	$dbconn->close();

	if (count($arts) < 1) return false;
	return $arts;
}


// 获取文章的标签
function get_labels_art($s_aid)
{
	// 连接数据库
	global $g_dbhost;
	global $g_dbname;
	global $g_dba_username;
	global $g_dba_password;

	if (trim($s_aid) == '') return false;

	$dbconn = new mysqli($g_dbhost,$g_dba_username,$g_dba_password);
	if($dbconn->errno){
		exit($dbconn->_errno);
	}

	$dbconn->query("SET NAMES 'UTF8'");
	$dbconn->query("SET CHARACTER SET UTF8");
	$dbconn->query("SET CHARACTER_SET_RESULTS=UTF8'");

	//选择数据库
	$dbconn->select_db($g_dbname);

	//执行MySQL语句
	$s_sql = "";
	$s_sql .= " select distinct label from tb_label_article where guid_art = '{$s_aid}'; ";
	$result=$dbconn->query($s_sql);
	if(!$result) {
		print($dbconn->errno);
		return false;
	}

	$labels = array();
	//提取数据
	if ($result) {
		while ( $row = $result->fetch_array()){
			$labels[] = $row;
		}
		// 释放资源 
		$result->free();
	}

	// 关闭连接 
	$dbconn->close();

	if (count($labels) < 1) return false;
	return $labels;
}

// 获取文章信息
function get_article($s_aid)
{
	// 连接数据库
	global $g_dbhost;
	global $g_dbname;
	global $g_dba_username;
	global $g_dba_password;

	if (trim($s_aid) == '') return false;

	$dbconn = new mysqli($g_dbhost,$g_dba_username,$g_dba_password);
	if($dbconn->errno){
		exit($dbconn->_errno);
	}

	$dbconn->query("SET NAMES 'UTF8'");
	$dbconn->query("SET CHARACTER SET UTF8");
	$dbconn->query("SET CHARACTER_SET_RESULTS=UTF8'");

	//选择数据库
	$dbconn->select_db($g_dbname);

	//执行MySQL语句
	$s_sql = "";
	$s_sql .= "	select * from tb_article where guid = '{$s_aid}' limit 0,1; ";
	
	$result=$dbconn->query($s_sql);
	if(!$result) {
		print($dbconn->errno);
		return false;
	}

	$article = array();
	//提取数据
	if ($result) {
		while ( $row = $result->fetch_array()){
			$article = $row;break;
		}
		// 释放资源 
		$result->free();
	}

	// 关闭连接 
	$dbconn->close();
	if (count($article) < 1) return false;
	return $article;
}

// 获取文章的读取次数
function get_nread_art($s_aid)
{
	// 连接数据库
	global $g_dbhost;
	global $g_dbname;
	global $g_dba_username;
	global $g_dba_password;

	if (trim($s_aid) == '') return false;

	$dbconn = new mysqli($g_dbhost,$g_dba_username,$g_dba_password);
	if($dbconn->errno){
		exit($dbconn->_errno);
	}

	$dbconn->query("SET NAMES 'UTF8'");
	$dbconn->query("SET CHARACTER SET UTF8");
	$dbconn->query("SET CHARACTER_SET_RESULTS=UTF8'");

	//选择数据库
	$dbconn->select_db($g_dbname);

	//执行MySQL语句
	$s_sql = "";
	$s_sql .= " select count(*) nread from tb_reader_art where guid_art = '{$s_aid}'; ";
	$result=$dbconn->query($s_sql);
	if(!$result) {
		print($dbconn->errno);
		return 0;
	}

	$nread = 0;
	//提取数据
	if ($result) {
		while ( $row = $result->fetch_array()){
			$nread = $row['nread'];
		}
		// 释放资源 
		$result->free();
	}

	// 关闭连接 
	$dbconn->close();

	return $nread;
}

// 添加新的文章读取记录
function add_reading($guid_art, $username, $userip)
{
	// 连接数据库
	global $g_dbhost;
	global $g_dbname;
	global $g_dba_username;
	global $g_dba_password;

	$dbconn = new mysqli($g_dbhost,$g_dba_username,$g_dba_password);
	if($dbconn->errno){
		exit($dbconn->_errno);
	}

	$dbconn->query("SET NAMES 'UTF8'");
	$dbconn->query("SET CHARACTER SET UTF8");
	$dbconn->query("SET CHARACTER_SET_RESULTS=UTF8'");

	//选择数据库
	$dbconn->select_db($g_dbname);

	//执行SQL语句
	$s_sql = '';
	$s_sql = ' insert into tb_reader_art(guid_art,username,userip, readtime) values(';
	$s_sql .= "'{$guid_art}'," . "'{$username}'," . "'{$userip}'," . "now()" . '); ';
	$dbconn->query("SET NAMES 'UTF8'");
	$result=$dbconn->query($s_sql);
	if(!$result) {
		print('>>> 添加文章读取记录失败，错误号：' . $dbconn->errno . ' <<<');
		return false;
	}
	
	// 关闭连接 
	$dbconn->close();

	return true;
}

// 添加新的评论
function add_comment($aid, $username, $content)
{
	// 连接数据库
	global $g_dbhost;
	global $g_dbname;
	global $g_dba_username;
	global $g_dba_password;

	$dbconn = new mysqli($g_dbhost,$g_dba_username,$g_dba_password);
	if($dbconn->errno){
		exit($dbconn->_errno);
	}

	$dbconn->query("SET NAMES 'UTF8'");
	$dbconn->query("SET CHARACTER SET UTF8");
	$dbconn->query("SET CHARACTER_SET_RESULTS=UTF8'");

	//选择数据库
	$dbconn->select_db($g_dbname);

	//执行SQL语句
	$userip = get_client_ip();
	$guid = guid();
	$s_sql = '';
	$s_sql .= ' insert into tb_comment(guid,guid_art,username,userip,content,maketime) values( ';
	$s_sql .= "'{$guid}'," . "'{$aid}'," . "'{$username}'," . "'{$userip}'," . "'{$content}'," . "now()" . '); ';
	$dbconn->query("SET NAMES 'UTF8'");
	$result=$dbconn->query($s_sql);
	if(!$result) {
		print('>>> 添加评论失败，错误号：' . $dbconn->errno . ' <<<');
		return '';
	}
	
	// 关闭连接 
	$dbconn->close();

	return $guid;
}

// 获取文章的评论列表
function get_comments_art($s_aid)
{
	// 连接数据库
	global $g_dbhost;
	global $g_dbname;
	global $g_dba_username;
	global $g_dba_password;

	if (trim($s_aid) == '') return false;

	$dbconn = new mysqli($g_dbhost,$g_dba_username,$g_dba_password);
	if($dbconn->errno){
		exit($dbconn->_errno);
	}

	$dbconn->query("SET NAMES 'UTF8'");
	$dbconn->query("SET CHARACTER SET UTF8");
	$dbconn->query("SET CHARACTER_SET_RESULTS=UTF8'");

	//选择数据库
	$dbconn->select_db($g_dbname);

	//执行MySQL语句
	$s_sql = "";
	$s_sql .= "select c.*,ifnull(u.avatar,'') avatar from			     ";
	$s_sql .= "(select * from tb_comment where guid_art = '{$s_aid}') c ";
	$s_sql .= "join												     ";
	$s_sql .= "(select * from tb_user) u							     ";
	$s_sql .= "on c.username = u.name								     ";
	$s_sql .= "order by c.maketime;     						         ";
	$result=$dbconn->query($s_sql);
	if(!$result) {
		print($dbconn->errno);
		return false;
	}

	$comments = array();
	//提取数据
	if ($result) {
		while ( $row = $result->fetch_array()){
			$comments[] = $row;
		}
		// 释放资源 
		$result->free();
	}

	// 关闭连接 
	$dbconn->close();

	if (count($comments) < 1) return false;
	return $comments;
}



// 添加新的用户
function add_new_user($username, $email, $password)
{
	// 连接数据库
	global $g_dbhost;
	global $g_dbname;
	global $g_dba_username;
	global $g_dba_password;

	$dbconn = new mysqli($g_dbhost,$g_dba_username,$g_dba_password);
	if($dbconn->errno){
		exit($dbconn->_errno);
	}

	$dbconn->query("SET NAMES 'UTF8'");
	$dbconn->query("SET CHARACTER SET UTF8");
	$dbconn->query("SET CHARACTER_SET_RESULTS=UTF8'");

	//选择数据库
	$dbconn->select_db($g_dbname);

	//执行SQL语句
	
	$s_sql = ' insert into tb_user(name,password,id_sex,avatar,email,homepage,introduction) ';
	$s_sql .= " select " . "'{$username}' name," . "'{$password}' password," . "1 id_sex," . "'/xblog/upload/default_avatar.png' avatar," .
		"'{$email}' email," ."'#' homepage," . "'我经验不多，大家多照顾！' introduction" . "; ";

	$dbconn->query("SET NAMES 'UTF8'");
	$result=$dbconn->query($s_sql);
	if(!$result) {
		//print($dbconn->errno);
		return false;
	}
	
	// 关闭连接 
	$dbconn->close();

	return true;
}

// 获取最热文章列表
function get_hot_arts($s_cate, $top)
{
	// 连接数据库
	global $g_dbhost;
	global $g_dbname;
	global $g_dba_username;
	global $g_dba_password;

	$dbconn = new mysqli($g_dbhost,$g_dba_username,$g_dba_password);
	if($dbconn->errno){
		exit($dbconn->_errno);
	}

	$dbconn->query("SET NAMES 'UTF8'");
	$dbconn->query("SET CHARACTER SET UTF8");
	$dbconn->query("SET CHARACTER_SET_RESULTS=UTF8'");

	//选择数据库
	$dbconn->select_db($g_dbname);
	
	//执行MySQL语句
	$s_sql = "";
	$s_sql .= " select c.guid_art guid, c.cnt, a.title from									  ";
	$s_sql .= " (																			  ";
	$s_sql .= "		select guid_art,count(guid_art)	 cnt from tb_reader_art group by guid_art ";
	$s_sql .= " ) c																			  "; 
	$s_sql .= " join																		  "; 
	$s_sql .= " (																			  "; 
	$s_sql .= "  	select * from tb_article where id_category like " . " '{$s_cate}'	      "; 
	$s_sql .= " ) a																			  "; 
	$s_sql .= " on c.guid_art = a.guid														  "; 
	$s_sql .= " order by cnt desc															  "; 
	$s_sql .= " limit 0, " . strval($top) . ";												  "; 
	
	$result=$dbconn->query($s_sql);
	if(!$result) {
		print($dbconn->errno);
		return false;
	}

	$arts = array();
	//提取数据
	if ($result) {
		while ( $row = $result->fetch_array()){
			$arts[] = $row;
		}
		// 释放资源 
		$result->free();
	}

	// 关闭连接 
	$dbconn->close();

	if (count($arts) < 1) return false;

	return $arts;
}

// 获取最热评论列表
function get_hot_comments($s_cate, $top)
{
	// 连接数据库
	global $g_dbhost;
	global $g_dbname;
	global $g_dba_username;
	global $g_dba_password;

	$dbconn = new mysqli($g_dbhost,$g_dba_username,$g_dba_password);
	if($dbconn->errno){
		exit($dbconn->_errno);
	}

	$dbconn->query("SET NAMES 'UTF8'");
	$dbconn->query("SET CHARACTER SET UTF8");
	$dbconn->query("SET CHARACTER_SET_RESULTS=UTF8'");

	//选择数据库
	$dbconn->select_db($g_dbname);

	//执行MySQL语句
	$s_sql = "";
	$s_sql .=  " select h.guid, h.cnt,c.username,u.avatar,c.content,c.maketime,guid_art,a.title title_art,a.id_category from "; 
	$s_sql .=  " (																											 "; 
	$s_sql .=  "  	(select guid, 1 cnt from tb_comment)																	 "; 
	$s_sql .=  "  	union all																								 "; 
	$s_sql .=  "  	(																										 "; 
	$s_sql .=  "  		select guid_comment guid, count(guid_comment) cnt from 												 "; 
	$s_sql .=  "  			tb_comment_comment group by guid_comment														 "; 
	$s_sql .=  "  	)																										 "; 
	$s_sql .=  " ) h																										 "; 
	$s_sql .=  " join																										 "; 
	$s_sql .=  " (																											 "; 
	$s_sql .=  "  	select * from tb_comment																				 "; 
	$s_sql .=  " ) c																										 "; 
	$s_sql .=  " on h.guid = c.guid																							 "; 
	$s_sql .=  " join																										 "; 
	$s_sql .=  " (																											 "; 
	$s_sql .=  "  	select * from tb_user																					 "; 
	$s_sql .=  " ) u																										 "; 
	$s_sql .=  " on c.username = u.name																						 "; 
	$s_sql .=  " join																										 "; 
	$s_sql .=  " (																											 "; 
	$s_sql .=  "  	select * from tb_article																				 "; 
	$s_sql .=  " ) a																										 "; 
	$s_sql .=  " on c.guid_art = a.guid																						 "; 
	$s_sql .=  " where a.id_category like '" . $s_cate . "'																	 "; 
	$s_sql .=  " order by h.cnt , c.maketime desc																			 "; 
	$s_sql .=  " limit 0, " . strval($top) . ";																				 "; 

	$result=$dbconn->query($s_sql);
	if(!$result) {
		print($dbconn->errno);
		return false;
	}

	$comments = array();
	//提取数据
	if ($result) {
		while ( $row = $result->fetch_array()){
			$row_cpy = $row;
			$row_cpy['content'] = preg_replace("/<(.*?)>/","",$row_cpy['content']);
			$row_cpy['content'] = substr($row_cpy['content'], 0, 300);
			$comments[] = $row_cpy;
		}
		// 释放资源 
		$result->free();
	}

	// 关闭连接 
	$dbconn->close();

	if (count($comments) < 1) return false;

	return $comments;
}

// 获取最热评论列表
function get_hot_labels($s_cate, $top)
{
	// 连接数据库
	global $g_dbhost;
	global $g_dbname;
	global $g_dba_username;
	global $g_dba_password;

	$dbconn = new mysqli($g_dbhost,$g_dba_username,$g_dba_password);
	if($dbconn->errno){
		exit($dbconn->_errno);
	}

	$dbconn->query("SET NAMES 'UTF8'");
	$dbconn->query("SET CHARACTER SET UTF8");
	$dbconn->query("SET CHARACTER_SET_RESULTS=UTF8'");

	//选择数据库
	$dbconn->select_db($g_dbname);

	//执行MySQL语句
	$s_sql = "";

	$s_sql .= " select l.label, count(*) cnt from "                   ;
	$s_sql .= " (select label, guid_art from tb_label_article) l "    ; 
	$s_sql .= " join											  "   ; 
	$s_sql .= " (select guid, id_category  from tb_article) a	  "   ; 
	$s_sql .= " on l.guid_art = a.guid							  "   ;
	$s_sql .= " where a.id_category like '" .$s_cate . "'         "   ;
	$s_sql .= " group by l.label            				      "   ;
	$s_sql .= " order by cnt desc limit 0, " . strval($top) . ";  "   ; 
	//print($s_sql);
	
	$result=$dbconn->query($s_sql);
	if(!$result) {
		print($dbconn->errno);
		return false;
	}

	$labels = array();
	//提取数据
	if ($result) {
		while ( $row = $result->fetch_array()){
			$labels[] = $row;
		}
		// 释放资源 
		$result->free();
	}

	// 关闭连接 
	$dbconn->close();

	if (count($labels) < 1) return false;

	return $labels;
}

// 获取文章分类列表
function get_obj_category($s_cateid)
{
	// 连接数据库
	global $g_dbhost;
	global $g_dbname;
	global $g_dba_username;
	global $g_dba_password;

	$dbconn = new mysqli($g_dbhost,$g_dba_username,$g_dba_password);
	if($dbconn->errno){
		exit($dbconn->_errno);
	}

	$dbconn->query("SET NAMES 'UTF8'");
	$dbconn->query("SET CHARACTER SET UTF8");
	$dbconn->query("SET CHARACTER_SET_RESULTS=UTF8'");

	//选择数据库
	$dbconn->select_db($g_dbname);

	//执行MySQL语句
	$s_sql = " select * from tb_category where id = '{$s_cateid}' limit 0,1; ";
	$result=$dbconn->query($s_sql);
	if(!$result) {
		print(mysql_error());
		return false;
	}

	$cate = array();
	//提取数据
	if ($result) {
		while ( $row = $result->fetch_array()){
			$cate = $row;break;
		}
		// 释放资源 
		$result->free();
	}

	// 关闭连接 
	$dbconn->close();
	if (count($cate) < 1) return false;
	return $cate;
}

// 添加文章态度
function add_attitue_article($s_aid, $username, $s_attitude)
{
	// 连接数据库
	global $g_dbhost;
	global $g_dbname;
	global $g_dba_username;
	global $g_dba_password;

	$dbconn = new mysqli($g_dbhost,$g_dba_username,$g_dba_password);
	if($dbconn->errno){
		exit($dbconn->_errno);
	}

	$dbconn->query("SET NAMES 'UTF8'");
	$dbconn->query("SET CHARACTER SET UTF8");
	$dbconn->query("SET CHARACTER_SET_RESULTS=UTF8'");

	//选择数据库
	$dbconn->select_db($g_dbname);

	//执行SQL语句
	$userip = get_client_ip();

	$s_sql = '';
	$s_sql .= ' insert into tb_attitude_article(guid_art,username,userip,attitude,maketime) values( ';
	$s_sql .= "'{$s_aid}'," . "'{$username}'," . "'{$userip}'," . "'{$s_attitude}'," . "now()" . '); ';
	$dbconn->query("SET NAMES 'UTF8'");
	$result=$dbconn->query($s_sql);
	if(!$result) {
		return -2;
	}
	
	// 关闭连接 
	$dbconn->close();

	return 0;
}

// 添加评论之评论
function add_comment_comment($s_cid, $username, $s_attitude)
{
	// 连接数据库
	global $g_dbhost;
	global $g_dbname;
	global $g_dba_username;
	global $g_dba_password;

	$dbconn = new mysqli($g_dbhost,$g_dba_username,$g_dba_password);
	if($dbconn->errno){
		exit($dbconn->_errno);
	}

	$dbconn->query("SET NAMES 'UTF8'");
	$dbconn->query("SET CHARACTER SET UTF8");
	$dbconn->query("SET CHARACTER_SET_RESULTS=UTF8'");

	//选择数据库
	$dbconn->select_db($g_dbname);

	//执行SQL语句
	$userip = get_client_ip();

	$s_sql = '';
	$s_sql .= ' insert into tb_comment_comment(guid_comment,username,userip,attitude,maketime) values( ';
	$s_sql .= "'{$s_cid}'," . "'{$username}'," . "'{$userip}'," . "'{$s_attitude}'," . "now()" . '); ';
	$dbconn->query("SET NAMES 'UTF8'");
	$result=$dbconn->query($s_sql);
	if(!$result) {
		return -2;
	}
	
	// 关闭连接 
	$dbconn->close();

	return 0;
}

// 获取文章的某种态度的数量（支持或者反对等等）
function get_attitude_article_number($s_aid, $s_attitude)
{
	// 连接数据库
	global $g_dbhost;
	global $g_dbname;
	global $g_dba_username;
	global $g_dba_password;

	$dbconn = new mysqli($g_dbhost,$g_dba_username,$g_dba_password);
	if($dbconn->errno){
		exit($dbconn->_errno);
	}

	$dbconn->query("SET NAMES 'UTF8'");
	$dbconn->query("SET CHARACTER SET UTF8");
	$dbconn->query("SET CHARACTER_SET_RESULTS=UTF8'");

	//选择数据库
	$dbconn->select_db($g_dbname);

	//执行MySQL语句
	if ($s_attitude == '支持') $s_attitude == 'support';
	if ($s_attitude == '反对') $s_attitude == 'oppose';
	$s_sql = "  select count(*) num from tb_attitude_article where attitude = '{$s_attitude}' and guid_art = '{$s_aid}' ; ";
	$result=$dbconn->query($s_sql);
	if(!$result) {
		print(mysql_error());
		print('出错啦！');
		return -100;
	}
	//print($s_sql);
	$num =99;
	//提取数据
	if ($result) {
		while ( $row = $result->fetch_array()){
			$num = $row[0];	break;
		}
		// 释放资源 
		$result->free();
	}

	// 关闭连接 
	$dbconn->close();

	return $num;
}

// 获取评论的态度数量（支持或者反对等等）
function get_attitude_comment_number($s_cid, $s_attitude)
{
	// 连接数据库
	global $g_dbhost;
	global $g_dbname;
	global $g_dba_username;
	global $g_dba_password;

	$dbconn = new mysqli($g_dbhost,$g_dba_username,$g_dba_password);
	if($dbconn->errno){
		exit($dbconn->_errno);
	}

	$dbconn->query("SET NAMES 'UTF8'");
	$dbconn->query("SET CHARACTER SET UTF8");
	$dbconn->query("SET CHARACTER_SET_RESULTS=UTF8'");

	//选择数据库
	$dbconn->select_db($g_dbname);

	//执行MySQL语句
	if ($s_attitude == '支持') $s_attitude == 'support';
	if ($s_attitude == '反对') $s_attitude == 'oppose';
	$s_sql = "  select count(*) num from tb_comment_comment where attitude = '{$s_attitude}' and guid_comment='{$s_cid}' ; ";
	$result=$dbconn->query($s_sql);
	if(!$result) {
		print(mysql_error());
		print('出错啦！');
		return -100;
	}
	//print($s_sql);
	$num =99;
	//提取数据
	if ($result) {
		while ( $row = $result->fetch_array()){
			$num = $row[0];	break;
		}
		// 释放资源 
		$result->free();
	}

	// 关闭连接 
	$dbconn->close();

	return $num;
}

?>
